//
//  WBVisitorView.swift
//  SinaMicroblog
//
//  Created by 原鹏飞 on 2017/4/2.
//  Copyright © 2017年 YuanPengFei. All rights reserved.
//

import UIKit

/** 自定义子视图的套路
 1. 创建一个继承自UIView的子视图文件
 2. 重写init方法
 3. 创建setupUI方法, 并在init方法中调用
 4. 创建子空间的属性
 5. 在setupUI的方法中做的事:
        添加控件
        设置控件
        自动布局
 */

//创建一个点击的代理
protocol WBVisitorViewDelegate: NSObjectProtocol {
    func login()
}

class WBVisitorView: UIView {
    //Swift定义代理属性的标准写法
    weak var delegate: WBVisitorViewDelegate?
    
    //MARK: - 懒加载属性
    //大图标
    lazy var iconImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    //大菊花
    lazy var circleImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    //遮罩视图
    lazy var maskImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    //Label
    lazy var titleLabel: UILabel = UILabel(title: "登录后, 别人评论你的微博, 发给你的消息, 都会在这里收到通知", alignment: .center)
    //注册按钮
    lazy var registButton: UIButton = UIButton(title: "注册", titleColor: UIColor.orange, bgImage: "common_button_white_disable", target: self, action: #selector(buttonClick))
    //登录按钮
    lazy var loginButton: UIButton = UIButton(title: "登录", bgImage: "common_button_white_disable", target: self, action: #selector(buttonClick))
    
    //保存访客视图的文字和图片的字典
    var visitorInfo: [String: Any]? {
        didSet {
            iconImageView.image = UIImage(named: visitorInfo?["imageName"] as! String)
            titleLabel.text = visitorInfo?["message"] as? String
            
            //判断菊花是否需要转动
            if let _ = visitorInfo?["isAnimation"] as? Bool {
                circleImageView.isHidden = false
            } else {
                circleImageView.isHidden = true
            }
        }
    }
    
    //MARK: - 重写init构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置UI
        setupUI()
    }
    //重写上面的init方法后, 会报一个带改正提示的大圆点警告
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


//MARK: - 事件处理
extension WBVisitorView {
    func buttonClick() {
        print("Good, I'm in!")
        delegate?.login()
    }
}


//MARK: - 设置UI
extension WBVisitorView {
    func setupUI() {
        self.backgroundColor = UIColor.rgbColor(r: 237, g: 237, b: 237)
        
        //1. 添加控件
        addSubview(iconImageView)
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(titleLabel)
        addSubview(registButton)
        addSubview(loginButton)
        
        //2. 自动布局
        //原则: 自上而下, 从左到右, 依次布局, 如果控件较多, 每布局完一两个都要运行下看看
        //大图标
        iconImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-100)
        }
        //大菊花
        circleImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconImageView)
            make.centerY.equalTo(iconImageView)
        }
        
        //遮罩视图
        maskImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconImageView)
            make.centerY.equalTo(iconImageView).offset(40)
            make.left.right.equalTo(self)
        }
        //文字Label
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconImageView)
            make.centerY.equalTo(iconImageView).offset(150)
            make.left.equalTo(self).offset(50)
            make.right.equalTo(self).offset(-50)
        }
        //注册按钮
        registButton.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(70)
            make.bottom.equalTo(self).offset(-120)
            make.size.equalTo(CGSize(width: 80, height: 35))
        }
        //登录按钮
        loginButton.snp.makeConstraints { (make) in
            make.right.equalTo(self).offset(-70)
            make.bottom.equalTo(self).offset(-120)
            make.size.equalTo(CGSize(width: 80, height: 35))
        }
        
        //调用函数: 添加动画
        addAnimation()
    }
    
    //添加大菊花动画
    func addAnimation() {
        //创建旋转动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = Double.pi * 2//旋转角度: 360度
        animation.duration = 15 //动画时长
        animation.repeatCount = MAXFLOAT    //动画执行次数: float的最大值
        animation.isRemovedOnCompletion = false //切换页面后, 再切回来仍然继续执行
        
        //将动画添加到Layer
        circleImageView.layer.add(animation, forKey: nil)
    }
}
